package com.hi.hiparty.solr;

import java.util.Collection;
import java.util.Date;

import org.apache.solr.client.solrj.SolrQuery;

public class PartySolrQuery extends SolrQuery {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2075713220903018251L;

	public PartySolrQuery addRangeFilter(String fq, String low, String high) {
		StringBuffer buf = new StringBuffer();
		buf.append(fq).append(":[").append(low).append(" TO ").append(high)
				.append("]");
		addFilterQuery(buf.toString());
		return this;
	}

	public PartySolrQuery addValueFilter(String fq, String value) {
		StringBuffer buf = new StringBuffer();
		buf.append(fq).append(":").append(value);
		addFilterQuery(buf.toString());
		return this;
	}
	public PartySolrQuery addValueFilter(String fq, Object[] values) {
		if(values==null || values.length==0)
			return this;
		for(Object value:values){
			StringBuffer buf = new StringBuffer();
			buf.append(fq).append(":").append(value.toString());
			addFilterQuery(buf.toString());
		}
		return this;
	}
	public PartySolrQuery addValueNotFilter(String fq, String value) {
		StringBuffer buf = new StringBuffer();
		buf.append("NOT ").append(fq).append(":").append(value);
		addFilterQuery(buf.toString());
		return this;
	}
	
	@SuppressWarnings("unchecked")
	public PartySolrQuery addOrValueFilter(String fq, Collection values) {
		if(values==null || values.isEmpty())
			return this;
		StringBuffer buf = new StringBuffer();
		for (Object value: values) {
			buf.append(fq + ":" + value.toString() + " OR ");
		}
		String rs = buf.substring(0, buf.length() - " OR ".length());
		addFilterQuery(rs);
		return this;
	}
	public PartySolrQuery addOrValueFilter(String fq, Object[] values) {
		if(values==null || values.length==0)
			return this;
		StringBuffer buf = new StringBuffer();
		for (Object value: values) {
			buf.append(fq + ":" + value.toString() + " OR ");
		}
		String rs = buf.substring(0, buf.length() - " OR ".length());
		addFilterQuery(rs);
		return this;
	}

	public PartySolrQuery setCollapseThreshold(int q) {
		this.set(CollapseParams.COLLAPSE_THRESHOLD, q);
		return this;
	}

	public PartySolrQuery setCollapseField(String q) {
		this.set(CollapseParams.COLLAPSE_FIELD, q);
		return this;
	}

	public PartySolrQuery setCollapseType(String q) {
		this.set(CollapseParams.COLLAPSE_TYPE, q);
		return this;
	}

	public PartySolrQuery setCollapseIncludeField(String q) {
		this.set(CollapseParams.COLLAPSE_INCLUDE_COLLAPSED_DOCS_FIELDS, q);
		return this;
	}
	public PartySolrQuery setCollapseDebugOn() {
		this.set(CollapseParams.COLLAPSE_DEBUG, "true");
		return this;
	}

	public PartySolrQuery addCollapseSortField(String field, ORDER order) {
		return addValueToParam(CollapseParams.COLLAPSE_SORT, toSortString(
				field, order));
	}

	private PartySolrQuery addValueToParam(String name, String value) {
		String tmp = this.get(name);
		tmp = join(tmp, value, ",");
		this.set(name, tmp);
		return this;
	}

	private String join(String a, String b, String sep) {
		StringBuilder sb = new StringBuilder();
		if (a != null && a.length() > 0) {
			sb.append(a);
			sb.append(sep);
		}
		if (b != null && b.length() > 0) {
			sb.append(b);
		}
		return sb.toString().trim();
	}

	private String toSortString(String field, ORDER order) {
		return field.trim() + ' ' + String.valueOf(order).trim();
	}

	public static String getRoomKey(String date){
		return date+ "_i";
	}



}
